<%--
  Created by IntelliJ IDEA.
  User: 12845
  Date: 2020/7/21
  Time: 15:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<%@ include file="admin-head.jsp" %>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<link rel="stylesheet" href="css/pagination.css">
<script type="text/javascript" src="crwod/my-role.js"></script>
<script type="text/javascript">
    $(function () {
        // 1.为分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";

        // 2.调用执行分页的函数，显示分页效果
        generatePage();

        // 3.给查询按钮绑定单击响应函数
        $("#searchBtn").click(function () {
            // ①获取关键词数据赋值给对应的全局变量
            window.keyword = $("#keywordInput").val();
            // ②调用分页函数刷新页面
            generatePage();
        });

        // 4.点击新增按钮打开模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show");
        });

        // 5.给新增模态框中的保存按钮绑定单击响应函数
        $("#saveRoleBtn").click(function () {
            // ①获取用户在文本框中输入的角色名称
            // #addModal表示找到整个模态框
            // 空格表示在后代元素中继续查找
            // [name=roleName]表示匹配name属性等于roleName的元素
            const roleName = $.trim($("#addModal [name = 'roleName']").val());

            // ②发送Ajax请求
            $.ajax({
                "url": "role/save.json",
                "type":"post",
                "dataType":"json",
                "data":{
                    "name":roleName
                },
                "success":function (response) {

                    const result = response.result;
                    if (result == "SUCCESS"){
                        layer.msg("新增成功!");
                        // 将页码定位到最后一页
                        window.pageNum = 2147483647;
                        // 重新加载分页数据
                        generatePage();
                    }else if(result == "FAILED") {
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function (response) {
                    layer.msg("新增失败!"+response.statusText);
                }
            });
            // 关闭模态框
            $("#addModal").modal('hide');
            // 清理模态框
            $("#addModal [name = 'roleName']").val("");

        });

        /*$("#showEditModalBtn").click(function () {
            $("#editModal").modal("show");
        });*/

        // 给页面上的“铅笔”按钮绑定单击响应函数，目的是打开模态框
        // 传统的事件绑定方式只能在第一个页面有效，翻页后失效了
        /*$(".pencil").click(function () {
            alert("1234");
        });*/

        // 使用jQuery对象的on()函数可以解决上面问题
        // ①首先找到所有“动态生成”的元素所附着的“静态”元素
        // ②on()函数的第一个参数是事件类型
        // ③on()函数的第二个参数是找到真正要绑定事件的元素的选择器
        // ③on()函数的第三个参数是事件的响应函数
        $("#rolePageBody").on("click",".pencil",function () {
            // 获取表格中当前行中的角色名称
            const roleName = $(this).parent().prev().text();

            // 获取当前角色的id
            // 依据是：var pencilBtn = "<button id='"+roleId+"' ……这段代码中我们把roleId设置到id属性了
            // 为了让执行更新的按钮能够获取到roleId的值，把它放在全局变量上
            window.roleId = this.id;

            // 使用roleName的值设置模态框中的文本框
            $("#editModal [name = 'roleName']").val(roleName);

            // 打开模态框
            $("#editModal").modal('show');
        });

        // 给更新模态框中的更新按钮绑定单击响应函数
        $("#updateRoleBtn").click(function () {
            const roleName = $("#editModal [name = 'roleName']").val();

            $.ajax({
                "url":"role/update.json",
                "type":"post",
                "dataType":"json",
                "data":{
                    "id": window.roleId,
                    "name": roleName
                },
               "success":function (response) {
                   const result = response.result;
                   if (result == "SUCCESS"){
                       layer.msg("修改成功!");

                       // 重新加载分页数据
                       generatePage();
                   }else if(result == "FAILED") {
                       layer.msg("操作失败！"+response.message);
                   }
               },
               "error":function (response) {
                   layer.msg("修改失败!"+response.statusText);
               }
            });

            $("#editModal").modal("hide");

        });

        // 临时测试代码
        //var roleArray =[{"roleId":5,"roleName":"aaa"},{"roleId":12,"roleName":"bbb"}];
        //showConfirmModal(roleArray);

        // 点击确认模态框中的确认删除按钮执行删除
        $("#removeRoleBtn").click(function () {

            // 从全局变量范围获取roleIdArray，转换为JSON字符串
            const requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url":"role/remove/by/role/id/array.json",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success":function (response) {
                    const result = response.result;
                    if (result == "SUCCESS"){
                        layer.msg("删除成功!");

                        // 重新加载分页数据
                        generatePage();
                    }else if(result == "FAILED") {
                        layer.msg("删除失败！"+response.message);
                    }
                },
                "error":function (response) {
                    layer.msg("删除失败!"+response.statusText);
                }
            });

            $("#confirmModal").modal("hide");
        })

        // 给单条删除按钮绑定单击响应函数
        $("#rolePageBody").on("click",".remove",function () {
            const roleId = this.id;
            const roleName = $(this).parent().prev().text();
            const roleArray = [{
                "roleId": roleId,
                "roleName": roleName
            }];
            showConfirmModal(roleArray);
        });


        // 给总的checkbox绑定单击响应函数
        $("#summaryBox").click(function () {
            const currentStatus = this.checked;
            $(".itemBox").prop("checked", currentStatus);
        });

        // 全选、全不选的反向操作
        $("#rolePageBody").on("click",".itemBox",function () {
            const checkedBoxCount = $(".itemBox:checked").length;
            const totalBoxCount = $(".itemBox").length;
            $("#summaryBox").prop("checked", checkedBoxCount == totalBoxCount);
        });

        // 给批量删除的按钮绑定单击响应函数
        $("#batchRemoveBtn").click(function () {
            const roleArray = [];
            $(".itemBox:checked").each(function () {
                const roleId = this.id;
                const roleName = $(this).parent().next().text();
                roleArray.push({
                    "roleId":roleId,
                    "roleName":roleName
                });
            });

            // 检查roleArray的长度是否为0
            const checkedBoxCount = $(".itemBox:checked").length;
            if (checkedBoxCount == 0){
                layer.msg("请至少选择一个执行删除");
                return;
            }
            showConfirmModal(roleArray);
        });

    });
</script>
<body>

<%@ include file="admin-nav.jsp" %>
<div class="container-fluid">
    <div class="row">
        <%@ include file="admin-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button id="showAddModalBtn" type="button" class="btn btn-primary" style="float:right;" <%--onclick="window.location.href='form.html'"--%>>
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <%--<button id="showEditModalBtn" type="button" class="btn btn-primary" style="float:right;" &lt;%&ndash;onclick="window.location.href='form.html'"&ndash;%&gt;>
                        <i class="glyphicon glyphicon-plus"></i> 测试修改
                    </button>--%>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            <%--<tr>
                                <td>1</td>
                                <td><input type="checkbox"></td>
                                <td>PM - 项目经理</td>
                                <td>
                                    <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
                                    <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>
                                    <button type="button" class="btn btn-danger btn-xs"><i class=" glyphicon glyphicon-remove"></i></button>
                                </td>
                            </tr>--%>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                    <%--<ul class="pagination">
                                        <li class="disabled"><a href="#">上一页</a></li>
                                        <li class="active"><a href="#">1 <span class="sr-only">(current)</span></a></li>
                                        <li><a href="#">2</a></li>
                                        <li><a href="#">3</a></li>
                                        <li><a href="#">4</a></li>
                                        <li><a href="#">5</a></li>
                                        <li><a href="#">下一页</a></li>
                                    </ul>--%>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

    <%@include file="modal-role-add.jsp"%>
    <%@include file="modal-role-edit.jsp"%>
    <%@include file="modal-role-confirm.jsp"%>

</body>
</html>